package com.travelsky.airchina.boarding.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.travelsky.airchina.boarding.domain.NewsColumn;
import com.travelsky.airchina.boarding.dto.NewsColumnView;
/**
 * 新闻栏目数据库接口的实现类
 * @author haichenwang
 *
 */
@Repository("newsColumnDAO")
public class NewsColumnDAO extends GenericDAOHibernate<NewsColumn,Long> {
	@Autowired
	public NewsColumnDAO(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	@SuppressWarnings("unchecked")
	public List<NewsColumn> findNewsColumnsByParentId(Long id) {
		String hql = null;
		if(id!=null){
			hql = "from NewsColumn where parentColumn.id = ? and status = ? order by sortOrder";
			return this.getHibernateTemplate().find(hql, id,"1");
		}else{
			 hql = "from NewsColumn where parentColumn is null and status = ? order by sortOrder";
			return this.getHibernateTemplate().find(hql,"1");
		}
	}

	@SuppressWarnings("unchecked")
	public boolean isNewsColumnCodeExist(String code) {
		String hql = "select count(*) from NewsColumn where code = ?";
		List<Long> list = this.getHibernateTemplate().find(hql,code);
		if(list!=null && list.size()>0){
			if(list.get(0)!=null && list.get(0)>=1){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public boolean isNewsColumnNameExist(String name) {
		String hql = "select count(*) from NewsColumn where name = ?";
		List<Long> list = this.getHibernateTemplate().find(hql,name);
		if(list!=null && list.size()>0){
			if(list.get(0)!=null && list.get(0)>0){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public boolean isNewsColumnNameUnique(String name) {
		String hql = "select count(*) from NewsColumn where name = ?";
		List<Long> list = this.getHibernateTemplate().find(hql,name);
		if(list!=null && list.size()>0){
			if(list.get(0)!=null && list.get(0)>=1){
				return false;
			}else{
				return true;
			}
		}else{
			return false;
		}
	}
	public List<NewsColumn> findAllParentNewsColumnsByChildId(Long id) {
		NewsColumn newsColumn = this.findById(id);
		List<NewsColumn> newsColumns = new ArrayList<NewsColumn>();
		findParentNewsColumnsByChildId(newsColumn,newsColumns);
		if(newsColumns.size()>1){
			Collections.reverse(newsColumns);
		}
		return newsColumns;
	}
	/**
	 * �ݹ��ѯ:ͨ����8Ŀ������������еĸ�8Ŀ
	 * @param newsColumn
	 * @param newsColumns
	 * @return
	 */
	private List<NewsColumn> findParentNewsColumnsByChildId(NewsColumn newsColumn,List<NewsColumn> newsColumns) {
		if(newsColumn==null){
			return newsColumns;
		}
		NewsColumn parenteNewsColumn = newsColumn.getParentColumn();
		if(parenteNewsColumn!=null){
			newsColumns.add(parenteNewsColumn);
			findParentNewsColumnsByChildId(parenteNewsColumn,newsColumns);
		}
		return newsColumns;
	}
	public void updateNewsColumnLeaf(Long id, String leafFlag) {
		String hql = "update NewsColumn set isLeaf = ? where id = ?";
		this.getHibernateTemplate().bulkUpdate(hql, new Object[]{leafFlag,id});
	}
	@SuppressWarnings("unchecked")
	public void updateNewsColumnLeaf(Long id) {
		String hql = "select count(id) from NewsColumn where parentColumn.id = ? ";
		List<Long> list = this.getHibernateTemplate().find(hql, id);
		long i = 0;
		if(list!=null && list.size()>0){
			if(list.get(0)!=null){
				i = list.get(0);
			}
		}
		if(i==0){
			this.getHibernateTemplate().bulkUpdate("update NewsColumn set isLeaf = ? where id = ?", new Object[]{"1",id});
		}
	}
	public int getNewsColumnSum() {
		String hql = "select count (id) from NewsColumn";
		return this.countHqlQuery(hql, null);
	}
	@SuppressWarnings("unchecked")
	public List<NewsColumn> findNewsColumnsByParentId(List<Long> newsColumnIds) {
		Map<String, List<Long>> map = Collections.singletonMap("newsColumnIds", newsColumnIds);
		String hql = "from NewsColumn where parentColumn.id in (:newsColumnIds) order by sortOrder";;
		Query query = this.getSession().createQuery(hql);
		if (map != null) {
			query.setProperties(map);
		}
		return query.list();
	}
	public boolean updateNewsColumnStatus(Long id, String status) {
		String hql = "update NewsColumn set status = ? where id = ?";
		int updateSum = this.getHibernateTemplate().bulkUpdate(hql,status,id);
		return updateSum>0?true:false;
	}
	public void deleteNewsByColumnIds(List<Long> ids) {
		String hql = "delete from News where newsColumn.id in (:ids) ";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("ids", ids, Hibernate.LONG);
		query.executeUpdate();
	}
	public void deleteVideoByColumnIds(List<Long> ids) {
		String hql = "delete from Video where column.id in (:ids)";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("ids", ids, Hibernate.LONG);
		query.executeUpdate();
	}
	@SuppressWarnings("unchecked")
	public void findAllColumnListByParentColumnId(Long pNewsColumnId,List<Long> allColumnIds) {
		String hql = "select id from NewsColumn where parentColumn.id =?";
		List<Long> childColumnIds = this.getHibernateTemplate().find(hql,pNewsColumnId);
		if(childColumnIds!=null&&childColumnIds.size()>0){
			for(Long temp:childColumnIds){
				allColumnIds.add(temp);
				findAllColumnListByParentColumnId(temp,allColumnIds);
			}
		}
	}
	public void deleteRoleColumnByColumnIds(List<Long> newsColumnIds) {
		String hql = "delete from RoleColumn where columnId in (:ids)";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("ids", newsColumnIds, Hibernate.LONG);
		query.executeUpdate();
	}
	@SuppressWarnings("unchecked")
	public List<NewsColumnView> findNewsColumnViewsByParentId(Long id) {
		String hql = null;
		List<NewsColumnView> list = new ArrayList<NewsColumnView>();
		List<Object[]> objs = null;
		if(id!=null){
			hql = "select id,parentColumn.id,name,isLeaf,isImageText,status from NewsColumn where parentColumn.id = ? and status = ? order by sortOrder";
			objs = this.getHibernateTemplate().find(hql, id,"1");
		}else{
			hql = "select id,parentColumn.id,name,isLeaf,isImageText,status from NewsColumn where parentColumn is null and status = ? order by sortOrder";
			objs = this.getHibernateTemplate().find(hql,"1");
		}
		if(objs!=null && objs.size()>0){
			for(Object[] obj:objs){
				NewsColumnView o = new NewsColumnView();
				o.setId((Long)obj[0]);
				o.setPId((Long)obj[1]);
				o.setName((String)obj[2]);
				o.setIsLeaf((String)obj[3]);
				o.setIsImageText((String)obj[4]);
				list.add(o);
			}
		}
		return list;
	}
}
